﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class ItemChild : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    DataSet dsImages = new DataSet();
    string cnt = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBind2();

        }
    }
    public string GetSmallContents(string str, int chars)
    {
        if (str.Length > chars)
        {
            str = str.Substring(0, chars);
            str = str + "...";
        }
        return str;
    }
    private void ListBind2()
    {
        string qry = "select * from tbItems where ItemID="+Convert.ToString(Request.QueryString["ItemID"]);

        if (Convert.ToString(Request.QueryString["ItemID"]) != null)
        {
            SqlDataAdapter adp = new SqlDataAdapter(qry, cnt);
            DataSet ds = new DataSet();
          
            adp.Fill(ds);
            lblGroupName.Text = ds.Tables[0].Rows[0]["ItemTitle"].ToString();
            DataList2.DataSource = ds;
            DataList2.DataBind();
        }
    }
    protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "ItemDetail")
        {
            Response.Redirect("SubItemDetail.aspx?SubItemID=" + e.CommandArgument);//         +"& NewsID="+aa);
        }
    }
    protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptDetails1 = (Repeater)e.Item.FindControl("TopNews1");

            if (rptDetails1 != null)
            {
                string query = "select  * from tbSubItems where ItemID=" + DataBinder.Eval(e.Item.DataItem, "ItemID").ToString();
                SqlDataAdapter adp = new SqlDataAdapter(query, cnt);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                //dsImages = SqlHelper.ExecuteDataset(con, CommandType.Text, query);
                if (ds != null)
                {
                    DataRow nRow = ds.Tables[0].NewRow();
                    nRow["SubItemThumbPath"] = @"Resources/AddButton.png";
                    ds.Tables[0].Rows.Add(nRow);
                    ds.GetChanges();
                    //  gv.DataSource = ds.Tables[0]; 
                    rptDetails1.DataSource = ds;
                    rptDetails1.DataBind();
                }
            }

        }
    }

    protected void linkButton_Item(object sender, EventArgs e)
    {
        var commandArgs = sender as LinkButton;
        var groupID = commandArgs.ClientID;
        string[] words = groupID.Split('_');
        string GroupID = words[2].ToString();
        //foreach (string word in words)
        //{
        //    var a = word;
        //}
        int a = Convert.ToInt32(GroupID) + 1;
        if (commandArgs.CommandArgument == "")
        {
            Response.Redirect("WebControls/Item_child.aspx?ItemID=" + a);

        }
        else
        {
            Response.Redirect("SubItemDetail.aspx?SubItemID=" + commandArgs.CommandArgument + "&ItemID=" + Request.QueryString["ItemID"].ToString());
            //Response.Redirect("SubItemDetail.aspx?SubItemID=" + commandArgs.CommandArgument);
        }
        // 


    }
    protected void backButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }
}